from CSVUtils.utils import read_csv
from gupiao.code_info import generate_info
from gupiao.day_trade import get_trade


def random_sleep():
    import time
    import random
    time.sleep(random.random() * 2 + 0)


def select_data():
    data = read_csv('output-2021-12-15.csv')
    # print(data)
    code_list = []
    for row in data:
        # 筛选跟随人数和市盈率和价格
        if row[-1] != 'None' and row[8] != 'None' and row[4] != 'None' and row[2] != 'None' and float(row[2]) < 20 and float(row[4]) >= 0:  # and row[1][0:3]!='*ST':
            code_list.append(row[0])
    return code_list


if __name__ == '__main__':
    code_list = select_data()
    print(len(code_list))
    dict1 = generate_info()

    count = 0
    print('代码', '名称', '最近8天', '最近15天', '最近30天', '今日涨幅', '股价', sep='\t')

    up = 2.5

    for code in code_list:
        div8, div15, div30, today_gain, today_price = get_trade(code)
        if div8 >= up or div15 >= up or div30 >= up:
            print(code, dict1[code], '%.2f' % div8, '%.2f' % div15, '%.2f' % div30, '%.2f' % today_gain, '%.2f' % today_price, sep='\t')
        #     count += 1
        # if count % 5 == 0:
        #     random_sleep()
        # if up_num_5 <= 3 and up_num_10 <= 5 and down_num_20 >= 13:
        #     print(code, dict1[code])
        count += 1
        if count % 10 == 0:
            random_sleep()
    print(code)
